#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    initModeSelect();
    initStackWidget();

//    initSqlOperator();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initSqlOperator()
{
    m_sqlWriter = new SqlOperator("test2", this);
    m_sqlWriter->readSqlData();

    //读数据
    RobotBaseData data = m_sqlWriter->getData();
    std::cout << data;
    //写数据
//    m_db1->writeData(data);

//    data = m_db1->getData();
    //打印数据
//    std::cout << data;
}

void MainWindow::initModeSelect()
{
    SelectItem* item1 = new SelectItem(":/icons/Icon/home_96_96.png",
                                       ui->modeSelect);
    SelectItem* item2 = new SelectItem(":/icons/Icon/data-sheet-100.png",
                                       ui->modeSelect);
    SelectItem* item3 = new SelectItem(":/icons/Icon/setting_96_96.png",
                                       ui->modeSelect);
    SelectItem* item4 = new SelectItem(":/icons/Icon/other1.png",
                                       ui->modeSelect);
    ui->modeSelect->addItem(item1);
    ui->modeSelect->addItem(item2);
    ui->modeSelect->addItem(item3);
    ui->modeSelect->addItem(item4);
}

void MainWindow::initDataView()
{
    m_dataView = new BDataView(this);
}

void MainWindow::initHomeView()
{
    m_homeView = new BHomeView(this);
}

void MainWindow::initStackWidget()
{
    //1.初始化界面
    initHomeView();
    initDataView();
    //2.stackWidget初始化
    ui->stackedWidget->insertWidget(0, m_homeView);
    ui->stackedWidget->insertWidget(1, m_dataView);
    //3.建立connect
    connect(ui->modeSelect, &ModeSelectWidget::itemChange, this, [&](int index){
       ui->stackedWidget->setCurrentIndex(index);
    });
    emit ui->modeSelect->itemChange(0);
}

void MainWindow::initTextOperator()
{
    m_textOperator = new TextOperator("C://Users//Beking//Desktop//"
                              "DHB//dhblab_meta\\recv_cache.txt", 100);

}

void MainWindow::setTextOperatorRunning(bool state)
{
    m_textOperator->setRunning(true);
    if (state) m_textOperator->start();
}


